﻿using Microsoft.AspNetCore.Mvc;
using RabbitMQ.Client;
using RabbitMQ_Common;
using System.Text;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RabbitMQ_Proider.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RabbitMQProController : ControllerBase
    {
        // GET: api/<RabbitMQProController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            var rounteKey = "test";
            int[] intlist = new int[] { 1, 2, 3,4,5,6,7,8,9 };
            var msg = "";
            using (var connection= RabbitMQHelper.GetConnection())
            {
                using (var channel =connection.CreateModel())//创建信道，通信管道
                {
                    //创建队列
                    channel.QueueDeclare(rounteKey, false, false, false, null);
                    //没绑定交换机，但由于rabbitmq有默认交换机
                    foreach(var row in intlist)
                    {
                        string message = row.ToString();
                        var body = Encoding.UTF8.GetBytes(message);
                        msg += message+",";
                        channel.BasicPublish("", rounteKey, null, body);
                    }
                    
                }
            }
                return new string[] { msg };
        }

        
    }
}
